package com.purong.purong.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.purong.purong.Contant;
import com.purong.purong.R;
import com.purong.purong.adapter.HelpQuestionAdapter;
import com.purong.purong.bean.ConfigBean;
import com.purong.purong.bean.HelpQuestionBean;
import com.purong.purong.bean.QuestionBean;
import com.purong.purong.widget.ConfirmDialog;
import com.purong.purong.widget.LoadingView;
import com.yao.sdk.net.ResponseInfo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import in.srain.cube.views.ptr.PtrCustomHandler;
import in.srain.cube.views.ptr.PtrDefaultCustomHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrPuRongLayout;

/**
 * Created by zengwenjie on 2017/12/7.
 */
public class HelpCatActivity extends BaseActivity {
    private RecyclerView mRecyclerView;
    private PtrPuRongLayout mPtrFrame;
    private LoadingView loadView;
    private HelpQuestionAdapter mAdapter;
    private List<QuestionBean> questionList=new ArrayList<>();
    private int page = 1;
    private String type;
    @Override
    public int getRootViewId() {
        return R.layout.activity_help_cat;
    }

    @Override
    public void initBundle() {
        type=getIntent().getStringExtra("TYPE");
    }

    @Override
    public void initView() {
        showBack();
        if (type!=null){
            switch (type){
                case "1":
                    showTitle("信用卡问题");
                    break;
                case "2":
                    showTitle("银行贷问题");
                    break;
                case "3":
                    showTitle("极速贷问题");
                    break;
                case "4":
                    showTitle("账号问题");
                    break;
            }
        }else {
            showTitle("相关问题");
        }
        loadView = (LoadingView)findViewById(R.id.loading_view);
        loadView.setNoResultToast("暂无问题", "");
        loadView.setLoadListener(new LoadingView.OnLoadListener() {
            @Override
            public void onReload() {
                getQuestion(true);
            }

            @Override
            public void onRefresh() {
                getQuestion(true);
            }
        });
        mRecyclerView=(RecyclerView)findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter=new HelpQuestionAdapter();
        mAdapter.setData(questionList);
        mAdapter.setOnQuestionListener(new HelpQuestionAdapter.OnQuestionListener() {
            @Override
            public void onClick(int position) {
                Intent intent=new Intent(HelpCatActivity.this,HelpQuestDetActivity.class);
                intent.putExtra("QUESTION",questionList.get(position));
                startActivity(intent);
            }
        });
        mRecyclerView.setAdapter(mAdapter);
        mPtrFrame = (PtrPuRongLayout) findViewById(R.id.rotate_header_list_view_frame);
        mPtrFrame.setPtrHandler(new PtrCustomHandler() {
            @Override
            public boolean checkCanDoLoadMore(PtrFrameLayout frame, View content, View footer) {
                if (questionList.size() == 0) {
                    return false;
                } else {
                    return PtrDefaultCustomHandler.checkContentCanBePulledUp(frame, content, footer);
                }

            }

            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                getQuestion(false);

            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getQuestion(true);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return false;
//                return (isRequesting||loanList.size()>0)?false:true;
            }
        });
    }
    private void getQuestion(boolean clear){
        Map<String, String> params = new LinkedHashMap<String, String>();
        if (clear){
            page=1;
            questionList.clear();
            mAdapter.notifyDataSetChanged();
            loadView.loading();
        }
        params.put("page", page+"");
        if (type!=null)
        params.put("question_type",type);
        if (!request(Contant.QUESTION, params, true)) {
            if (clear)
                loadView.fail();
            if (!hasDestroy())
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        if (mPtrFrame.isRefreshing()) {
                            mPtrFrame.refreshComplete();
                        }
                    }
                }, 100);
        }
    }
    @Override
    public void initData() {
        getQuestion(true);
    }
    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.QUESTION)){
            if (mPtrFrame.isRefreshing()) {
                mPtrFrame.refreshComplete();
            }
            if (responseInfo.isRequestOk){
                try{
                    HelpQuestionBean bean= JSON.parseObject(responseInfo.content,HelpQuestionBean.class);
                    if (bean != null && bean.getQuestion_list() != null) {
                        if (bean.getQuestion_list().size() > 0)
                            page++;
                        questionList.addAll(bean.getQuestion_list());
                        mAdapter.notifyDataSetChanged();

                    }
                    if (questionList.size() == 0) {
                        loadView.noResultWithRefresh();

                    } else {
                        loadView.finish();
                    }
                }catch (Exception e){
                    if (page == 1) {
                        loadView.fail();
                    }
                }
            }else {
                if (page == 1) {
                    loadView.fail();
                }
            }
        }
    }
}
